//格式化时间
function dateFtt(fmt,date)   
{ //author: meizz   
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
} 
function formatSubscribeTime(cellValue, options, rowObject) {
	var dateStr = "";
	if(cellValue){
		var crtTime = new Date(cellValue);
		dateStr = dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);
	}
    return dateStr;
}
//加载字典
function dictJudge(value, options, row){
    //sex为自定义字典项
	var statusObj = vm.options.sex;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'roadlive/roadlive/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'ID', width: 50, key: true, hidden:true },
			{ label: '区域', name: 'area', index: 'AREA', width: 50 ,
				formatter:function (cellvalue) {
					if (cellvalue === 'stq'){return '山亭区';}
					if (cellvalue === 'xcq'){return '薛城区';}
					if (cellvalue === 'szq'){return '市中区';}
					if (cellvalue === 'ycq'){return '峄城区';}
					if (cellvalue === 'tez'){return '台儿庄';}
					if (cellvalue === 'gxq'){return '高新区';}
					if (cellvalue === 'gsgl'){return '高速公路';}
					if (cellvalue === 'tzs'){return '滕州市';}
				}
			},
			{ label: '设备名称', name: 'devName', index: 'DEV_NAME', width: 80 },
			{ label: '道路名称', name: 'roadName', index: 'ROAD_NAME', width: 80 },
			{ label: '播放地址', name: 'playUrl', index: 'PLAY_URL', width: 80 },
			{ label: '是否可用', name: 'enable', index: 'ENABLE', width: 80 ,
				formatter:function (cellvalue) {
					return cellvalue === 1 ? "可用" : "不可用";
				}
			},
			{ label: '创建时间', name: 'createTime', index: 'CREATE_TIME', width: 80,
				formatter:function (cellvalue) {
					if (!cellvalue) {
						return "";
					}
					return dateFtt("yyyy-MM-dd hh:mm:ss", new Date(cellvalue));
                }
			},
			{ label: '修改时间', name: 'updateTime', index: 'UPDATE_TIME', width: 80,
				formatter:function (cellvalue) {
					if (!cellvalue) {
						return "";
					}
					return dateFtt("yyyy-MM-dd hh:mm:ss", new Date(cellvalue))
                }
			}
			// { label: '数据添加时间', name: 'addTime', index: 'ADD_TIME', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

//初始化日期组件-年月日
function datePlugin(){
    $('#collection_time').datepicker({
        language: 'zh-CN',//显示中文
        startView: 0, //选择器开始界面 0：天（默认 1：月2：年3：十年 4：世纪
        minViewMode: 0,//最小选择精度 0：天（最小默认）
        maxViewMode: 2, //最大选择精度 4：世纪（最大默认）
        format: 'yyyy-mm-dd',//显示格式
        minView: "day",//设置只显示到月份
        // initialDate: new Date(),
        autoclose: true,//选中自动关闭
        // todayBtn: true//显示今日按钮
        clearBtn: true, //清除按钮
        todayHighlight: true //今日高亮
    });
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			area:''
		},
		showList: true,
		title: null,
		roadLive: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.roadLive = {};

            datePlugin();
            //设置默认日期
            // vm.roadLive.collectionTime = dateFtt('yyyy-MM-dd',new Date());
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.roadLive.id == null ? "roadlive/roadlive/save" : "roadlive/roadlive/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.roadLive),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "roadlive/roadlive/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "roadlive/roadlive/info/"+id, function(r){
                vm.roadLive = r.roadLive;

                //日期格式化
                // vm.roadLive.collectionTime = dateFtt("yyyy-MM-dd",new Date(vm.roadLive.collectionTime));
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'area': encodeURIComponent(vm.q.area)},
                page:page
            }).trigger("reloadGrid");
		}
	}
});